package com.smartalarmclock.Model;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SmartAlarmClockDBHelper extends SQLiteOpenHelper {

	public SmartAlarmClockDBHelper(Context context) {
		super(context, "SmartAlarmClock.db", null, 1);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SmartAlarmClockDB.GetSqlStatementCreateReferencesTable());
		db.execSQL(Contact.GetSqlStatementCreateContactTable());
		db.execSQL(Group.GetSqlStatementCreateGroupToContactsTable());
		db.execSQL(Snooze.GetSqlStatementCreateSnoozeTable());
		db.execSQL(AlarmClock.GetSqlStatementCreateAlarmClockTable());
		db.execSQL(SMSMessage.GetSqlStatementCreateSMSMessageTable());
		db.execSQL(AutoResponse.GetSqlStatementCreateAutoResponseTable());
		db.execSQL(TimeFrame.GetSqlStatementCreateTimeFrameTable());
		db.execSQL(RulesItem.GetSqlStatementCreateRulesItemTable());
		db.execSQL(RulesItem.GetSqlStatementCreateRulesItemToTimeFramesTable());
		db.execSQL(VIP.GetSqlStatementCreateVIPTable());
		db.execSQL(Profile.GetSqlStatementCreateProfileTable());
		db.execSQL(Profile.GetSqlStatementCreateProfileToContactsTable());
		db.execSQL(LogEntry.GetSqlStatementCreateLogTable());
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO: Maybe we should create an identical temp table, insert all existing records,
		//		 delete the original table, create a new one and copy the existing record?
		
		// Drop table in the reverse order of the table creation in onCreate
		db.execSQL(LogEntry.GetSqlStatementDropLogTable());
		db.execSQL(Profile.GetSqlStatementDropProfileToContactsTable());
		db.execSQL(Profile.GetSqlStatementDropProfileTable());
		db.execSQL(VIP.GetSqlStatementDropVIPTable());
		db.execSQL(RulesItem.GetSqlStatementCreateRulesItemToTimeFramesTable());
		db.execSQL(RulesItem.GetSqlStatementDropRulesItemTable());
		db.execSQL(TimeFrame.GetSqlStatementDropTimeFrameTable());
		db.execSQL(AutoResponse.GetSqlStatementDropAutoResponseTable());
		db.execSQL(SMSMessage.GetSqlStatementDropSMSMessageTable());
		db.execSQL(AlarmClock.GetSqlStatementDropAlarmClockTable());
		db.execSQL(Snooze.GetSqlStatementDropSnoozeTable());
		db.execSQL(Group.GetSqlStatementDropGroupToContactsTable());
		db.execSQL(Contact.GetSqlStatementDropContactTable());
		db.execSQL(SmartAlarmClockDB.GetSqlStatementDropReferencesTable());
		
		onCreate(db);
	}
}
